// pages/inspectionCompany/inspectionCompany.js
//index.js 
//获取应用实例 
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hospitalList: [],
    dataList: [],
    name: '江苏',
    canShow: false,
    indexVersion: '',
    canShowlbmtName: false,
    TypeInfo: '全部',
    sortStr: '',
    lbmtNumber: '',
    problem: ['低电', '锁钩开关', '蓝牙连接不上', '超期未巡检', '在桩上7天'],
    zhanshi: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.getHospitalInfo()
  },
  // 跳转到医院详情
  jumpInfo(e) {
    wx.navigateTo({
      url: '../hospitalDetail/hospitalDetail?hospitalId=' + e.currentTarget.dataset.id,
    })
  },
  changezhanshi() {
    this.setData({
      zhanshi: !this.data.zhanshi
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //记录安装师傅点击问题汇总和巡检汇总的操作
  recordInstallersOptions(){
    var that = this
    lbbService.recordInstallersOptions({
      province: that.data.name?that.data.name:'',
      city: that.data.ctname?that.data.ctname:''
    },function(res){
      that.getCityList()
    })
  },
  // 获取省份
  getCity(e) {
    this.setData({
      name: e.detail,
      ctname: '',
      zhanshi:false
    })
    this.recordInstallersOptions()
  },
  // 获取城市
  getCt(e) {
    this.setData({
      ctname: e.detail,
    })
    this.recordInstallersOptions()
  },
  getHospitalInfo(e) {
    var that = this;
    lbbService.getHospitalProblemsSummary({
      sortType: that.data.sortStr,
      lbmTypeId: that.data.lbmtNumber
    }, function (res) {
      var cityArr = []
      var problemArr = []
      that.setData({
        hospitalList: res.data.problemsSummaryInfos,
        sortMap: res.data.sortMap,
        indexVersion: that.data.indexVersion ? that.data.indexVersion : res.data.sortMap.latest,
        mechanismTypeInfos: res.data.mechanismTypeInfos,
        TypeInfo: that.data.TypeInfo ? that.data.TypeInfo : res.data.mechanismTypeInfos[0].lbmtName,
      })
      that.data.hospitalList.forEach((item) => {
        cityArr.push(item.province)
        let pArr = item.problemTags.split(",");
        pArr.forEach(function(err) {
          problemArr.push(err);
        });
      })
      var cityArray = new Set(cityArr)
      var nproblemArr = new Set(problemArr)
      var arrA = [...cityArray]
      var nproblemArrA = [...nproblemArr]
      var resultArray = arrA.sort(
        function (param1, param2) {
          return param1.localeCompare(param2, "zh");
        }
      );
      that.setData({
        cityArray: resultArray,
        problem: nproblemArrA
      })
      that.recordInstallersOptions()
    })
  },
  // 获取城市的list
  getCityList() {
    var that = this
    var smallCityArr = []
    that.data.dataList = that.data.hospitalList.filter((item) => {
      return item.province == that.data.name
    })
    that.setData({
      dataList: that.data.dataList
    })
    // 新增城市列表
    that.data.cityDataList = that.data.dataList.filter((item) => {
      smallCityArr.push(item.city)
    })
    var ctarray = new Set(smallCityArr)
    console.log('ctarray', ctarray)
    // var nproblemArr = new Set(problemArr)
    var arrA = [...ctarray]
    console.log('arrA', arrA)
    // var nproblemArrA = [...nproblemArr]
    var resultctArray = arrA.sort(
      function (param1, param2) {
        return param1.localeCompare(param2, "zh");
      }
    );
    that.setData({
      ctArray: resultctArray,
      // ctname: that.data.ctname? that.data.ctname:resultctArray[0],
    })
    if (that.data.ctname) {
      // 筛选出 市
      that.data.dataList = that.data.dataList.filter((item) => {
        return item.city == that.data.ctname
      })
    }
    that.setData({
      dataList: that.data.dataList
    })
    //  ------------------------------------------------------------------
    var problemArr = []
    that.data.dataList.forEach((item) => {
      let errArr = item.problemTags.split(',');
      errArr.forEach((err)=>{
        problemArr.push(err);
      })
    });
    var nproblemArr = new Set(problemArr)
    var nproblemArrA = [...nproblemArr]
    that.setData({
      problem: nproblemArrA
    })
  },
  changeGroup(e) {
    var that = this
    var newArr = []
    that.data.dataList = that.data.hospitalList.filter((item) => {
      return item.province == that.data.name
    })
    if (e.detail.value.length > 0) {
      that.data.dataList.forEach((item) => {
        e.detail.value.forEach((item1) => {
          let isContainErr = (item.problemTags && item.problemTags.indexOf(item1) != -1) || item.firstAppearDesc == item1;
          if (isContainErr) {
            newArr.push(item)
          }
        })
      })
      that.setData({
        dataList: newArr
      })
    } else {
      that.setData({
        dataList: that.data.dataList
      })
    }

  },
  selectTap() {
    this.setData({
      canShow: !this.data.canShow
    })
    if (this.data.canShowlbmtName) {
      this.setData({
        canShowlbmtName: false
      })
    }

  },
  selectLbmTap() {
    this.setData({
      canShowlbmtName: !this.data.canShowlbmtName
    })
    if (this.data.canShow) {
      this.setData({
        canShow: false
      })
    }
  },
  optionVersionTap(e) {
    var that = this
    let Index = e.currentTarget.dataset.index;
    let sortStr = e.currentTarget.dataset.str;
    console.log(Index, e)
    that.setData({
      indexVersion: Index,
      sortStr: sortStr,
      canShow: !that.data.canShow
    });
    that.getHospitalInfo()
  },
  optionlbmTap(e) {
    var that = this
    let Index = e.currentTarget.dataset.index;
    let lbmtNumber = e.currentTarget.dataset.number
    that.setData({
      TypeInfo: Index,
      lbmtNumber: lbmtNumber,
      canShowlbmtName: !that.data.canShowlbmtName
    });
    that.getHospitalInfo()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})